<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <style>
    #list {
      display: flex;
      list-style: none;
      justify-content: center;
      text-align: center;
    }
    #btn {
      position: relative;
      left: 650px;
    }
    #btns {
      position: relative;
      left: 800px;
    }
    
  </style>
  <body>
    <div id="box">
      <div id="list"></div>
    </div>
    <button id="btn">点击渲染</button>
    <button id="btns">撤销渲染</button>
    <script>
      var bb = (document.querySelector("#btn").onclick = function () {
        // 1. 创建一个xhr实例对象
        var xhr = new XMLHttpRequest();
        xhr.open("GET", "./05_data.php");
        xhr.send();
        xhr.onreadystatechange = function () {
          if (xhr.readyState === 4) {
            var json = xhr.responseText;
            // console.log(json);
            json = JSON.parse(json);
            var html = "";
            for (var i = 0; i < json.length; i++) {
              html += `
                                          <li>
                                                <img src="${json[i].img}">
                                                <p>${json[i].title}</p>
                                          </li><br>
                                    `;
                                  
            }
          }
          document.querySelector("#list").innerHTML = html;
        };
      });
      // 点击进入后让按钮消失
      document.querySelector("#btns").onclick = function (arr) {
        document.querySelector("#list").innerHTML = "";
      };
    </script>
  </body>
</html>
